﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System.IO;
using LiteFetion.Utils;

namespace LiteFetion
{
    /// <summary>
    /// 
    /// </summary>
    public class LogManager
    {
        private object obj = new object();
        private FileStream fs;
        public FetionClient FetionClient { get; private set; }
        public string FileName
        {
            get;
            set;
        }

        public LogManager(FetionClient client)
        {
            this.FetionClient = client;
            string directory = Util.MapPath("/Log/" + client.User.Account);
            this.FileName = Path.Combine(directory, System.DateTime.Now.ToString("yyyy-MM-dd") + ".txt");
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
        }

        public void Log(string msg)
        {
            lock (obj)
            {
                StreamWriter sw = null;
                fs = new FileStream(this.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                fs.Seek(0, SeekOrigin.End);
                sw = new StreamWriter(fs, System.Text.Encoding.Default);
                sw.WriteLine(msg);
                sw.Close();
                sw = null;
                fs.Close();
                fs = null;
            }
        }
    }
}
